package com.deyuanyun.pic.ctrl.service.pushmsg;

import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.ctrl.enumeration.CurrentFerenceEnum;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspAcMiorRecordExcel;
import com.deyuanyun.pic.ctrl.excelImport.PicCtrlExinspDcMiorRecordExcel;
import com.deyuanyun.pic.settings.controller.dto.BizMsgThresholdVO;
import com.deyuanyun.pic.settings.domain.base.BizMsgUser;
import com.deyuanyun.pic.settings.enumeration.ThresholdType;
import com.deyuanyun.pic.settings.service.msgtemplate.AbstractPushMsgTemplate;
import com.deyuanyun.pic.settings.service.msgtemplate.MsgQueue;
import com.deyuanyun.pic.settings.support.utils.PipeDictUtils;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *  腐蚀控制---交流干扰/直流干扰监测记录数据录入消息推送
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-10-21
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
public class ExinspAcDcMiorRecordPushMsg extends AbstractPushMsgTemplate {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExinspAcDcMiorRecordPushMsg.class);

    private String createRemark;//创建备注

    public static enum AcDcMiorEnum {
        AC, //交流干扰
        DC; //直流干扰
    }

    @Override
    public void doPushMsgData() {
        if(super.interfaceParam.isEmpty()) {
            LOGGER.info("没有传入参数");
            return;
        }

        String type = (String) super.interfaceParam.get(CtrlPushMsgCons.TYPE_KEY);

        Object obj = super.interfaceParam.get(CtrlPushMsgCons.LIST_KEY);

        if(obj != null && (obj instanceof List)) {
            BizMsgThresholdVO msgThresholdVO = null;

            if(AcDcMiorEnum.AC.toString().equals(type)) {
                msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_AC_CURRENT_DENSITY_INPUT.getValue());
            }
            if(AcDcMiorEnum.DC.toString().equals(type)) {
                msgThresholdVO = super.bizMsgThresholdService.queryDataByTypeOne(ThresholdType.INTERNAL_DC_CURRENT_DENSITY_INPUT.getValue());
            }

            if(msgThresholdVO == null) {
                LOGGER.info("没有查询到阀值信息");
                return;
            }
            Object objThre = msgThresholdVO.getThresholdContentJson();
            if(objThre == null || objThre == "" || !(objThre instanceof Map)) {
                LOGGER.info("没有设置对应的字段值或者不是map结构");
                return;
            }
            Map thresholdMap = (Map) objThre;

            String interference = (String) thresholdMap.get("interference");//业务阀值中设置的“干扰强度”值
            if(StringUtils.isEmpty(interference)) { //没有设置对应的值
                return;
            }
            List<String> strs = Lists.newArrayList(CurrentFerenceEnum.BIG.toString(),CurrentFerenceEnum.ORDINARY.toString(), CurrentFerenceEnum.LITTLE.toString());

            if(strs.contains(interference)) { //判断页面业务阀值设置的选项的值在不在strs集合里面
                List list = (List) obj;
                for (int i = 0; i < list.size(); i++) {
                    String interferenceVoltage = "";//干扰程度
                    String pipeLineId = ""; //管道id
                    String monitorPointName = ""; //监测点名称

                    if(AcDcMiorEnum.AC.toString().equals(type)) {
                        PicCtrlExinspAcMiorRecordExcel recordExcel = (PicCtrlExinspAcMiorRecordExcel) list.get(i);
                        monitorPointName = recordExcel.getMonitorPointName();
                        pipeLineId = recordExcel.getPipeLineId();
                        interferenceVoltage = recordExcel.getAcInterferenceVoltage();
                        createRemark = "交流干扰检测记录录入消息推送";
                    }
                    if(AcDcMiorEnum.DC.toString().equals(type)) {
                        PicCtrlExinspDcMiorRecordExcel recordExcel = (PicCtrlExinspDcMiorRecordExcel) list.get(i);
                        interferenceVoltage = recordExcel.getAcInterferenceVoltage();
                        monitorPointName = recordExcel.getMonitorPointName();
                        pipeLineId = recordExcel.getPipeLineId();
                        createRemark = "直流干扰检测记录录入消息推送";
                    }
                    if(interference.equalsIgnoreCase(interferenceVoltage) && StringUtils.isNotEmpty(pipeLineId)
                            && StringUtils.isNotEmpty(monitorPointName)) { //判断页面业务阀值设置的和excel填写的是否一值
                        String pipeLineName = PipeDictUtils.getLineById(pipeLineId).getName();
                        super.thresListCastString(Lists.newArrayList(pipeLineName, monitorPointName));
                        initializeBizMsgSends(msgThresholdVO);
                    }
                }
            }

        }

    }

    /**
     * @param args 参数说明：
     * <pre>
     * arg[0]：为业务阀值实体信息，类型为：{@link BizMsgThresholdVO}<br />
     * </pre>
     */
    @Override
    public void initializeBizMsgSends(Object... args) {
        BizMsgThresholdVO msgThresholdVO = (BizMsgThresholdVO) args[0];
        super.roleList = msgThresholdVO.getRoleIdList();
        String msgContent = msgThresholdVO.getMsgContent();

        if (super.roleList != null && !super.roleList.isEmpty()) {
            Set<String> userIds = super.bizMsgThresholdService.queryUseIdByRoleIds(super.roleList); //用户id集合
            List<MsgQueue.BizMsgUserInfo> userInfos = new ArrayList<MsgQueue.BizMsgUserInfo>();

            for (String userId : userIds) {
                BizMsgUser bizMsgUser = new BizMsgUser();
                bizMsgUser.setCreateRemark(this.createRemark);
                bizMsgUser.setCreateDate(new Date());
                if (StringUtils.isNotEmpty(msgContent))//消息内容不为空的时候设置到消息推送实体类
                    bizMsgUser.setMsgContent(msgContent);
                if (StringUtils.isNotEmpty(super.thresholdContent)) { //设置阀值内容
                    bizMsgUser.setThresholdContent(super.thresholdContent);
                }
                bizMsgUser.setUserId(userId);
                bizMsgUser.setId(CommonUtil.getUUID());
                userInfos.add(msgQueue.new BizMsgUserInfo().setValid(false).setBizMsgUser(bizMsgUser));
            }
            if(!userInfos.isEmpty()) {
                //封装好需要发送的消息信息交给队列来推送消息
                MsgQueue.MsgInfo msgInfo = msgQueue.new MsgInfo().setBizMsgThresholdId(msgThresholdVO.getId())
                        .setThresholdSet(msgThresholdVO.getThresholdContent()).setRoleIds(super.roleList);
                msgQueue.addOperator(msgQueue.new PushMsgOperator(userInfos, msgInfo));
            }
        }
    }

    @Override
    public boolean valiIsPush(Map<String, Object> valiMap) {
        return false;
    }
}
